package org.dtrd.modules.evaluation.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.SdscaInfo;
import org.dtrd.modules.evaluation.entity.dto.SdscaDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdSdsca;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdSdscaMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdSdscaService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.StringJoiner;

/**
 * <p>
 * 自我管理评估（SDSCA） 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdSdscaServiceImpl extends ServiceImpl<DtrdEntEvaluationRdSdscaMapper, DtrdEntEvaluationRdSdsca> implements IDtrdEntEvaluationRdSdscaService {

    @Override
    public SdscaInfo saveSdscaInfo(SdscaDTO dto) {
        // 计算各字段分数 以及组合 input 字段
        StringJoiner joiner = new StringJoiner(",");
        int scoreDietTotal = 0;
        int scoreExerciseTotal = 0;
        int scoreMedicationTotal = 0;
        int scoreMonitoringTotal = 0;
        int scoreFootCheckTotal = 0;
        int tmpScore;
        for (int i = 0; i < dto.getAnswers().length; i++) {
            // sdsca 12道题的分值都是
            tmpScore = dto.getAnswers()[i];
            joiner.add(tmpScore + "");
            if (2 == i) {
                tmpScore = 7 - i;
            }
            switch (i) {
                case 0:
                case 1:
                case 2:
                    scoreDietTotal += tmpScore;
                    break;
                case 3:
                case 4:
                    scoreExerciseTotal += tmpScore;
                    break;
                case 5:
                    scoreMedicationTotal += tmpScore;
                    break;
                case 6:
                case 7:
                    scoreMonitoringTotal += tmpScore;
                    break;
                case 8:
                case 9:
                case 10:
                case 11:
                    scoreFootCheckTotal += tmpScore;
                    break;
                default:
                    break;
            }
        }
        float scoreDiet = BigDecimal.valueOf((float) scoreDietTotal / 3).setScale(1, RoundingMode.HALF_UP).floatValue();
        float scoreExercise = BigDecimal.valueOf((float) scoreExerciseTotal / 2).setScale(1, RoundingMode.HALF_UP).floatValue();
        float scoreMedication = -1;
        if (-1 != scoreMedicationTotal) {
            scoreMedication = BigDecimal.valueOf((float) scoreMedicationTotal).setScale(1, RoundingMode.HALF_UP).floatValue();
        }
        float scoreMonitoring = BigDecimal.valueOf((float) scoreMonitoringTotal / 2).setScale(1, RoundingMode.HALF_UP).floatValue();
        float scoreFootCheck = BigDecimal.valueOf((float) scoreFootCheckTotal / 4).setScale(1, RoundingMode.HALF_UP).floatValue();
        float scoreTotal = scoreDiet + scoreExercise + scoreMedication + scoreMonitoring + scoreFootCheck;

        // 设置输出文字
        // 自我管理评分分级方法: 好: 总分> 29 分, 单项> 5.6 分。
        // 一般: 总分21~ 29分, 单项者5.6~ 4.2分。
        // 差: 总分< 21 分,单项< 4.2分。
        // 按照盛医生建议，这里只根据单项判断
        float scoreHigh = 5.6f;
        float scoreLow = 4.2f;
        String output = "您的总分为"+ NumberUtil.round(scoreTotal, 1) +"分，其中，饮食"+scoreDiet+"分，运动"+scoreExercise+"分，用药"
                +scoreMedication+"分，血糖监测"+scoreMonitoring+"分。足部检查"+scoreFootCheck+"分。自我管理水平";
        if (scoreDiet > scoreHigh && scoreExercise > scoreHigh && (scoreMedication > scoreHigh || -1 == scoreMedication) && scoreMonitoring > scoreHigh
                && scoreFootCheck > scoreHigh) {
            output += "较好。";
        } else if (scoreDiet >= scoreLow && scoreExercise >= scoreLow && (scoreMedication >= scoreLow || -1 == scoreMedication)
                && scoreMonitoring >= scoreLow && scoreFootCheck >= scoreLow) {
            output += "适中。";
        } else {
            output += "不理想。";
        }

        // 保存到数据库
        DtrdEntEvaluationRdSdsca sdsca = new DtrdEntEvaluationRdSdsca();
        sdsca.setInput(joiner.toString());
        sdsca.setCreateTime(new Date());
        sdsca.setScoreDiet(scoreDiet);
        sdsca.setScoreExercise(scoreExercise);
        sdsca.setScoreMedication(scoreMedication);
        sdsca.setScoreMonitoring(scoreMonitoring);
        sdsca.setScoreFootcheck(scoreFootCheck);
        sdsca.setScoreTotal(scoreTotal);
        sdsca.setOutput(output);
        if (dto.getPatientId() != null) {
            sdsca.setPatientId(dto.getPatientId());
            save(sdsca);
        }
        return new SdscaInfo().parseFromPo(sdsca);
    }

    @Override
    public SdscaInfo getSdscaInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdSdsca> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdSdsca::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdSdsca::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdSdsca::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdSdsca sdsca = baseMapper.selectOne(lambdaQueryWrapper);
        SdscaInfo sdscaInfo = new SdscaInfo();
        if (sdsca != null) {
            sdscaInfo = sdscaInfo.parseFromPo(sdsca);
        }
        return sdscaInfo;
    }
}
